// ! 分页功能
let pagenum = 1
//  ! 渲染我的点赞
function renderCollect() {
  axios.get('/user/userDataList?page=1&limit=10&type=1')
    .then(({
      data: {
        data
      }
    }) => {
      // console.log(data)
      const {
        count
      } = data.list

      const rendering = data.list.data.map((e) => {
        const {
          author,
          id,
          click,
          tags,
          title,
          lovenum,
          description,
          pic,
          ishot
        } = e
        let picIng = `http://124.223.14.236:8060/${pic}`
        return `
        <article class="excerpt excerpt-one">
        <header>
          ${ishot===1?'<a class="cat label label-important" href="javascript:;">热门<i class="label-arrow"></i></a>':''}
          <h2>
            <a target="_blank" href="show.html" title="阿里云拼团推荐服务器">${title}</a>
          </h2>
        </header>
        <p class="text-muted time">
          <a href="">${author}·编辑</a> 发布于 2018-03-30
        </p>
        <p class="focus">
          <a href="http://blog.weknow.cn/474.shtml" class="thumbnail">
            <span class="item"><span class="thumb-span"><img src="${pic===null?'./images/default.png':picIng}"
                  class="thumb" /></span></span></a>
        </p>
        <p class="note">${description===null?"":description}</p>
        <p class="text-muted views">
          <span class="post-views">阅读(${click===null?"0":click})</span>
          <span class="post-comments">评论(0)</span>
          <a href="javascript:;" class="post-like">
            <i class="fa fa-thumbs-up"></i>赞 (<span>${lovenum}</span>)
          </a>
          ${tags===null?"":`<span class="post-tags">标签：<a href="" rel="tag">${tags}</a></span>`}
          <button class="btn btn-xs btn-danger" data-id='${id}'><i class="fa fa-trash"></i>&nbsp;取消点赞</button>
        </p>
      </article>
        `
      }).join('')

      $('.list-box').html(rendering)
      renderfen(count)
    })
}
renderCollect()


 // todo  渲染分页功能函数
function renderfen(count){
  layui.use(function () {
    // 开启 HASH
    laypage.render({
      elem: 'aa',
      count: count,
      theme: '#1E9FFF',
      curr: pagenum, // 初始获取 hash 值为 curr 的当前页
      layout: ['prev', 'page', 'next', 'count','refresh'],
      jump: function (obj, first) {
        //  得到当页值
        pagenum = obj.curr
        // 判断下一页 下一页是否显示
        if (pagenum === 1) {
          document.querySelector('.layui-laypage-prev').style.display = 'none'
        } else if (document.querySelector('.layui-disabled')) {
          document.querySelector('.layui-disabled').style.display = 'none'
        }
        //首次不执行
        if (!first) {
          //do somethingre
          renderCollect(obj.curr)
        }
      }
    });
  });
}


//  ! 取消我的点赞
// 事件委托获取id
$('.list-box').on('click', 'button', function () {
  const idZan = $(this).data('id')
  console.log(idZan)
  axios.get(`/user/userDataHandle?type=1&action=del&article_id=${idZan}`)
    .then((res) => {
      const {
        data,
        errno
      } = res.data
      if (errno !== 0) {
        return layer.msg('取消点赞失败')
      }
      // 成功
      layer.msg(data, {
        icon: 1,
        time: 500 // 设置 2 秒后自动关闭
      }, function () {
        // 关闭后 跳转操作
        location.href = '../public/user-collect.html'
      })
    })
})